package com.oim.fx.ui.head;

import com.oim.fx.common.NinePatch;

import javafx.geometry.Rectangle2D;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Slider;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Background;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Rectangle;

/**
 * @author XiaHui
 * @date 2017年6月16日 下午8:29:23
 */
public class ImageCropperPane extends StackPane {

	BorderPane rootPane = new BorderPane();
	StackPane stackPane = new StackPane();

	StackPane pane = new StackPane();

	StackPane coverImagePane = new StackPane();
	ImageView coverImageView = new ImageView();

	// StackPane anchorPane = new StackPane();
	AnchorPane anchorPane = new AnchorPane();

	ScrollPane scrollPane = new ScrollPane();

	StackPane imagePane = new StackPane();
	ImageView imageView = new ImageView();

	NinePatch ninePatch;
	Slider slider = new Slider();

	boolean newImage = false;
	Image image;
	// private ScrollBar vScrollBar;
	// private ScrollBar hScrollBar;
	VBox vBox = new VBox();

	public ImageCropperPane() {
		initComponent();
		initEvent();
	}

	private void initComponent() {
		// Image image = new
		// Image(getClass().getResource("/resources/common/images/cropper/CircleMask.png").toExternalForm());
		// ninePatch = new NinePatch(image, 1, 1, 1, 1);

		// coverImagePane.widthProperty().addListener(w -> {
		// //coverImageView.setFitWidth(coverImagePane.getWidth());
		// coverImageView.setImage(ninePatch.generate((int)
		// coverImagePane.getWidth(), (int) coverImagePane.getHeight()));
		// });
		// coverImagePane.heightProperty().addListener(h -> {
		// //coverImageView.setFitHeight(coverImagePane.getHeight());
		// coverImageView.setImage(ninePatch.generate((int)
		// coverImagePane.getWidth(), (int) coverImagePane.getHeight()));
		// });

		// coverImageView.setImage(image);
		imageView.setPreserveRatio(true);
		imagePane.getChildren().add(imageView);
		// anchorPane.getChildren().add(imagePane);

		coverImagePane.getChildren().add(coverImageView);
		coverImagePane.setMouseTransparent(true);

		scrollPane.setBackground(Background.EMPTY);
		// scrollPane.setVvalue(20);
		scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
		scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
		// scrollPane.setContent(imagePane);
		scrollPane.setContent(anchorPane);

		Rectangle clip = new Rectangle();
		clip.setWidth(350);
		clip.setHeight(350);

		anchorPane.getChildren().add(imagePane);
		// anchorPane.setClip(clip);
		// anchorPane.setMaxSize(350, 350);

		// pane.getChildren().add(anchorPane);
		pane.getChildren().add(scrollPane);
		pane.getChildren().add(coverImagePane);

		HBox bottomBox = new HBox();

		HBox leftBottomBox = new HBox();
		HBox rightBottomBox = new HBox();
		slider.setMin(100);
		leftBottomBox.getChildren().add(slider);

		bottomBox.getChildren().add(leftBottomBox);
		bottomBox.getChildren().add(rightBottomBox);

		stackPane.getChildren().add(pane);

		vBox.getChildren().add(stackPane);
		vBox.getChildren().add(bottomBox);

		StackPane sp = new StackPane();
		sp.getChildren().add(vBox);
		// rootPane.setCenter(stackPane);
		// rootPane.setBottom(bottomBox);

		this.getChildren().add(sp);
	}

	private double initX;
	private double initY;

	private void initEvent() {
		imagePane.heightProperty().addListener(h -> {
			if (newImage) {
				newImage = false;

				double min = coverImagePane.getWidth();
				double max = imagePane.getWidth();

				double temp = max - min;

				if (temp < 100) {
					max = min + (100 - temp);
				}

				slider.setMax(max);
				slider.setMin(min - 2);
				slider.setValue(slider.getMax());
			}
		});

		slider.valueProperty().addListener(l -> {
			imageView.setFitWidth(slider.getValue());
		});

		imagePane.setOnMousePressed((MouseEvent me) -> {
			initX = me.getX() - imagePane.getLayoutX();
			initY = me.getY() - imagePane.getLayoutY();
		});

//		double x = 0;
//		double y = 0;

		imagePane.setOnMouseDragged((MouseEvent me) -> {
			imagePane.setLayoutX(me.getX() - initX);
			imagePane.setLayoutY(me.getY() - initY);
			// me.getX();
			// me.getY();
			me.consume();
		});
	}

	@Override
	protected void layoutChildren() {
		super.layoutChildren();
	}

	public void setCoverImage(Image coverImage) {
		coverImageView.setImage(coverImage);
	}

	public void setCoverSize(double width, double height) {
		coverImageView.setFitWidth(width);
		coverImageView.setFitHeight(height);
		pane.setPrefSize(width, height);
		pane.setMaxSize(width, height);
		vBox.setMaxWidth(width);
	}

	public void setImage(Image image) {
		this.image = image;
		imageView.setImage(image);
		if (null != image) {
			newImage = true;
		}
	}

	public boolean hasImage() {
		return this.image != null;
	}

	public Image getImage() {
		Rectangle2D viewport = new Rectangle2D(0, 0, scrollPane.getWidth(), scrollPane.getHeight());
		SnapshotParameters p = new SnapshotParameters();
		p.setViewport(viewport);
		WritableImage image = scrollPane.snapshot(p, null);
		return image;// imageView.getImage();
	}
}
